package com.junci.mtest.model.http;

import android.text.TextUtils;


import com.junci.mtest.model.Constants;
import com.junci.mtest.model.bean.MySelfInfo;
import com.junci.mtest.utils.ACache;
import com.junci.mtest.utils.LogUtil;

import java.io.File;
import java.util.Map;
import java.util.Set;

/**
 * 给网络请求做缓存的专用类
 */

public class HttpCache {

    private static ACache httpCache1;
    private static String cacheId;

    private HttpCache() {

    }

    private static final HttpCache httpCache = new HttpCache();

    public static HttpCache getInstance() {
        return httpCache;
    }

    public String getCache(String url, Map<String, String> map) {
//        LogUtil.s("~~~~~~~~~~~~~~~~~~~缓存~~~getCache~~~~~~~~~~~~url~~~~~~getUid~~~~~~~~~~~~~~~~~~~~~~~~~~~" + url + DeviceInfoUtil.getUid());
        // 获取缓存
        httpCache1 = ACache.get(new File(Constants.FILE_BASE + File.separator + "cache" + File.separator + MySelfInfo.getInstance().getId()));
        if (cacheId != null) {
            if (!cacheId.equals(MySelfInfo.getInstance().getId())) {
                httpCache1 = null;
                if (LogUtil.isLog()) {
                    LogUtil.s("~~~~~缓存~~~~~cacheId~~"+cacheId);
                    LogUtil.s("~~~~~缓存~~~~~getId~~"+MySelfInfo.getInstance().getId());
                }
            }
        }
        String asString = "";
        try {
            String prama = "";
            if (map != null) {
                try {
                    Set<Map.Entry<String, String>> entries = map.entrySet();
                    for (Map.Entry<String, String> entry : entries) {
                        String key = entry.getKey();
                        String value = entry.getValue();
                        prama += key + "=" + value + "&";
                    }
                    prama = prama.substring(0, prama.length() - 1);
                } catch (Exception e) {
//                    e.printStackTrace();
                }
            }
//            if (httpCache1 == null) {
//                if (!TextUtils.isEmpty(MySelfInfo.getInstance().getId())) {
//                    httpCache1 = ACache.get(new File(Constants.FILE_BASE + File.separator + "cache" + File.separator + MySelfInfo.getInstance().getId()));
//                    cacheId = MySelfInfo.getInstance().getId();
//                }
//            }
            if (httpCache1 != null) {
                asString = httpCache1.getAsString(url + "?" + prama);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return asString;
    }

    public void saveData(String url, Map<String, String> map, String string) {
        LogUtil.s("~~~~~~~~~~~~~~~~~~~缓存~~~saveData~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//        LogUtil.s("~~~~~~~~~~~~~~~~~~~缓存~~~saveData~~~~~~url~~~~~~getUid~~~~~~~~~~~~~~~~~~~~~~~~~~~" + url + DeviceInfoUtil.getUid());
        // 根据url和map 将string存储起来
        if (cacheId != null) {
            if (!cacheId.equals(MySelfInfo.getInstance().getId())) {
                httpCache1 = null;
            }
        }
        try {
            String prama = "";
            if (map != null) {
                try {
                    Set<Map.Entry<String, String>> entries = map.entrySet();
                    for (Map.Entry<String, String> entry : entries) {
                        String key = entry.getKey();
                        String value = entry.getValue();
                        prama += key + "=" + value + "&";
                    }
                    prama = prama.substring(0, prama.length() - 1);
                } catch (Exception e) {
//                    e.printStackTrace();
                }
            }
            if (httpCache1 == null) {
                if (!TextUtils.isEmpty(MySelfInfo.getInstance().getId())) {
                    httpCache1 = ACache.get(new File(Constants.FILE_BASE + File.separator + "cache" + File.separator + MySelfInfo.getInstance().getId()));
                    cacheId = MySelfInfo.getInstance().getId();
                }
            }
            if (httpCache1 != null) {
                httpCache1.put(url + "?" + prama, string);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void cleanAll() {
        if (httpCache1 != null) {
            httpCache1.clear();
        }
    }
}
